package com.cqqec.boot.service.impl;

import com.cqqec.boot.entity.Dish;
import com.cqqec.boot.mapper.DishMapper;
import com.cqqec.boot.service.DishService;
import com.cqqec.boot.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    DishMapper DishMapper;

    /**
     * 获取所有菜品
     * @return
     */
    @Override
    public List<Dish> getAllDish() {
        return DishMapper.getAllDish();
    }
    @Override
    public PageUtil<Dish> getDishByLimit(int current, int size) {
        //分页的逻辑
        PageUtil<Dish> page = new PageUtil<>();
        //page对象中设置相关的值
        page.setCurrent(current);//当前页码
        page.setSize(size);//每页显示的条数
        //计算分页偏移量
        int offset = (current-1)*size;
        List<Dish> list= DishMapper.getDishByLimit(offset,size);//查询分页数据
        page.setList(list); //设置分页数据
        //查询表中总的数据条数
        page.setTotal(DishMapper.getCount());
        //向上取整
        int end = (int)Math.ceil((double) page.getTotal()/size);
        page.setEnd(end);//设置分页的结束页码
        return page;
    }
    /**
     * 根据id获取菜品
     * @param id
     * @return
     */
    @Override
    public Dish getDishById(int id) {
        return DishMapper.getDishById(id);
    }

    /**
     * 修改菜品
     * @param dish
     * @return
     */
    @Override
    public int updateDish(Dish dish) {
        return DishMapper.updateDish(dish);
    }
    /**
     * 根据id删除
     * @param id
     * @return
     */
    @Override
    public int deleteById(int id) {
        return DishMapper.deleteById(id);
    }
/**
 * 添加菜品
 * @param dish
 * @return
 */
    @Override
    public int addDish(Dish dish) {
        return DishMapper.addDish(dish);
    }

    /**
     * 修改菜品状态
     * @param dish
     * @return
     */
    @Override
    public int updateStatus(Dish dish) {
        return DishMapper.updateStatus(dish);
    }

}
